package com.example.pt.service.impl;


import com.example.pt.entity.Torrent;
import com.example.pt.repository.TorrentRepository;
import com.example.pt.service.RecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RecommendationServiceImpl implements RecommendationService {

    @Autowired
    private TorrentRepository torrentRepository;

    @Override
    public List<Torrent> getTimeBasedRecommendations(int limit) {
        LocalDate today = LocalDate.now();
        DayOfWeek dayOfWeek = today.getDayOfWeek();

        String category = (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)
                ? "tv"
                : "movie";

        List<Torrent> categoryList = torrentRepository.findRecommendedByCategory(category, limit);
        List<Torrent> topDownloadedList = torrentRepository.findTopDownloaded(5);
        List<Torrent> randomList = torrentRepository.findRandomTorrents(5); // ✅ 随机推荐

        // 合并去重
        Set<Long> seenIds = new HashSet<>();
        List<Torrent> result = new ArrayList<>();

        for (Torrent t : categoryList) {
            if (seenIds.add(t.getId())) {
                result.add(t);
            }
        }

        for (Torrent t : topDownloadedList) {
            if (seenIds.add(t.getId())) {
                result.add(t);
            }
        }

        for (Torrent t : randomList) {
            if (seenIds.add(t.getId())) {
                result.add(t);
            }
        }

        return result;
    }



}